package org.wangchaocheng.cloudgallery.user.gatewayimpl;

import java.util.List;
import java.util.Objects;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.support.SFunction;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.apache.commons.collections4.CollectionUtils;
import org.apache.commons.lang3.StringUtils;
import org.springframework.stereotype.Repository;
import org.wangchaocheng.cloudgallery.common.utils.IBatisPlusWrapperBuilder;
import org.wangchaocheng.cloudgallery.dto.common.PageDTO;
import org.wangchaocheng.cloudgallery.dto.user.RoleDTO;
import org.wangchaocheng.cloudgallery.enums.user.RoleStatusEnum;
import org.wangchaocheng.cloudgallery.query.user.RolePageQry;
import org.wangchaocheng.cloudgallery.user.converter.RoleConverter;
import org.wangchaocheng.cloudgallery.user.dataObject.RoleDO;
import org.wangchaocheng.cloudgallery.user.gateway.RoleGateway;
import org.wangchaocheng.cloudgallery.user.mapper.RoleMapper;

/**
 * @author wangchaocheng
 * @date 2025-03-01 10:30:19
*/
@Repository
public class RoleGatewayImpl extends ServiceImpl<RoleMapper, RoleDO> implements RoleGateway {

    @Override
    public void save(RoleDTO roleDTO) {
        RoleDO roleDO = RoleConverter.INSTANCE.dto2Do(roleDTO);
        RoleDTO existRole = this.findByColumn(RoleDTO::getIdentifier, roleDTO.getIdentifier());
        if (Objects.nonNull(existRole)) {
            roleDO.setId(existRole.getId());
            this.baseMapper.updateById(roleDO);
        } else {
            this.baseMapper.insert(roleDO);
        }
    }

    @Override
    public <T> RoleDTO findByColumn(SFunction<RoleDTO, ?> column, T value) {
        QueryWrapper<RoleDO> queryWrapper = IBatisPlusWrapperBuilder.buildSignCloumnQueryWrapper(column, new RoleDO(), value);
        if (Objects.isNull(queryWrapper)){
            return null;
        }
        RoleDO roleDO = baseMapper.selectOne(queryWrapper);
        return RoleConverter.INSTANCE.do2Dto(roleDO);
    }

    @Override
    public <T> Boolean existByColumn(SFunction<RoleDTO, ?> column, T value) {
        QueryWrapper<RoleDO> queryWrapper = IBatisPlusWrapperBuilder.buildSignCloumnQueryWrapper(column, new RoleDO(), value);
        if (Objects.nonNull(queryWrapper)){
            return exists(queryWrapper);
        }
        return Boolean.FALSE;
    }

    @Override
    public List<RoleDTO> listAllEnableRole() {
        LambdaQueryWrapper<RoleDO> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(RoleDO::getStatus, RoleStatusEnum.NORMAL);
        List<RoleDO> roleList = baseMapper.selectList(queryWrapper);
        return RoleConverter.INSTANCE.doList2DtoList(roleList);
    }

    @Override
    public List<RoleDTO> listByRoleIds(List<Long> roleIds) {
        if (CollectionUtils.isEmpty(roleIds)){
            return List.of();
        }
        LambdaQueryWrapper<RoleDO> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(RoleDO::getStatus, RoleStatusEnum.NORMAL);
        queryWrapper.in(RoleDO::getId, roleIds);
        List<RoleDO> roleList = baseMapper.selectList(queryWrapper);
        return RoleConverter.INSTANCE.doList2DtoList(roleList);

    }

    @Override
    public PageDTO<RoleDTO> listPage(RolePageQry qry) {
        // 查询条件
        LambdaQueryWrapper<RoleDO> queryWrapper = new LambdaQueryWrapper<RoleDO>()
            .like(StringUtils.isNotBlank(qry.getRoleName()), RoleDO::getRoleName, qry.getRoleName())
            .like(StringUtils.isNotBlank(qry.getDescription()), RoleDO::getDescription, qry.getDescription())
            .eq(StringUtils.isNotBlank(qry.getStatus()), RoleDO::getStatus, qry.getStatus());
        // 排序
        if (StringUtils.isNotBlank(qry.getOrderBy())){
            IBatisPlusWrapperBuilder.appendDynamicOrderColumn(queryWrapper, RoleDO.class, qry.getOrderBy(), qry.isOrderByAsc());
        }
        // 查询分页
        Page<RoleDO> roleDOPage = baseMapper.selectPage(new Page<>(qry.getPageIndex(), qry.getPageSize()), queryWrapper);
        return RoleConverter.INSTANCE.toPageDTO(roleDOPage);
    }
}




